import java.util.*;

public class Customer {

    private String password;
    private String ID;
    private String name;
    private String level;
    private String registeredDate;
    private double totalSpent;
    private String phone;
    private String email;
    private Map<String, Integer> cart;
    private List<String> purchaseHistory;
    private int loginAttempts = 0;

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getID() {
        return ID;
    }

    public void setID(String ID) {
        this.ID = ID;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLevel() {
        return level;
    }

    public void setLevel(String level) {
        this.level = level;
    }

    public String getRegisteredDate() {
        return registeredDate;
    }

    public void setRegisteredDate(String registeredDate) {
        this.registeredDate = registeredDate;
    }

    public double getTotalSpent() {
        return totalSpent;
    }

    public void setTotalSpent(double totalSpent) {
        this.totalSpent = totalSpent;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Customer() {
        this.cart = new HashMap<>();
        this.purchaseHistory = new ArrayList<>();
        this.loginAttempts = 0;
    }

    public Customer(String id, String name, String level, String registeredDate, double totalSpent, String phone, String email, String password) {
        this.ID = id;
        this.name = name;
        this.level = level;
        this.registeredDate = registeredDate;
        this.totalSpent = totalSpent;
        this.phone = phone;
        this.email = email;
        this.password = password;
    }

    public boolean register(String username, String userPassword, String userEmail) {
        if (username.length() < 5 || userPassword.length() <= 8 || !isValidEmail(userEmail)) {
            return false;
        }
        ID = username;
        password = userPassword;
        email = userEmail;
        Manager.addCustomer(this); // 添加到管理器中的客户列表
        return true;
    }

    private boolean isValidEmail(String email) {
        return email.contains("@");
    }

    public void login() {
        System.out.println("~~~~~登录系统~~~~~");
        System.out.println("请输入账号");
        Scanner scanner = new Scanner(System.in);
        this.ID = scanner.next();
        System.out.println("请输入密码");
        this.password = scanner.next();
        for (Customer customer : Manager.customers) {
            if (customer.getID().equals(ID) && customer.getPassword().equals(password)) {
                System.out.println("登录成功，欢迎您,用户：" + customer.getName());
                ObjectSystem.customerMenu();
                return;
            }
        }
        System.out.println("账号或者密码错误，请重新登录");
        login();
    }

    public void changePassword(String newPassword) {
        if (newPassword.length() > 8) {
            password = newPassword;
        } else {
            System.out.println("密码不符合要求");
        }
    }

    public void resetPassword(String username, String useremail) {
        if (ID.equals(username) && email.equals(useremail)) {
            password = generateRandomPassword();
            System.out.println("新密码已发送至邮箱： " + useremail);
            ObjectSystem.customerMenu();
        } else {
            System.out.println("用户名或邮箱错误");
        }
    }

    private String generateRandomPassword() {
        return "NewPassword123!";
    }

    public void addToCart(String productId, int quantity) {
        cart.put(productId, quantity);
        ObjectSystem.customerMenu();
    }

    public void removeFromCart(String productId) {
        if (cart.containsKey(productId)) {
            System.out.println("请确认是否继续移除操作");
            Scanner scanner = new Scanner(System.in);
            String input = scanner.nextLine();
            if (input.equalsIgnoreCase("yes")) {
                cart.remove(productId);
                System.out.println("移除成功");
                ObjectSystem.customerMenu();
            }
        } else {
            System.out.println("购物车中没有该商品");
            ObjectSystem.customerMenu();
        }
    }

    public void updateProductQuantity(String productId, int newQuantity) {
        if (newQuantity <= 0) {
            cart.remove(productId);
            ObjectSystem.customerMenu();
        } else {
            cart.put(productId, newQuantity);
            ObjectSystem.customerMenu();
        }
    }

    public void makePayment() {
        System.out.println("模拟支付操作成功，购买的商品数量已更新");
        for (Map.Entry<String, Integer> entry : cart.entrySet()) {
            purchaseHistory.add("购买时间：" + new Date() + "，商品编号：" + entry.getKey() + "，数量：" + entry.getValue());
        }
        cart.clear();
        ObjectSystem.customerMenu();
    }

    public void viewPurchaseHistory() {
        System.out.println("购物历史：");
        for (String history : purchaseHistory) {
            System.out.println(history);
        }
        ObjectSystem.customerMenu();
    }
}